package lunadevs.luna.module.exploits;

import org.lwjgl.input.Keyboard;

import com.darkmagician6.eventapi.EventTarget;
import com.zCore.Core.zCore;

import lunadevs.luna.category.Category;
import lunadevs.luna.events.BoundingBoxEvent;
import lunadevs.luna.events.InsideBlockRenderEvent;
import lunadevs.luna.events.PacketRecieveEvent;
import lunadevs.luna.events.PacketSendEvent;
import lunadevs.luna.events.PushOutOfBlocksEvent;
import lunadevs.luna.events.UpdateEvent;
import lunadevs.luna.main.Luna;
import lunadevs.luna.module.Module;
import lunadevs.luna.utils.ClientUtils;
import lunadevs.luna.utils.TimeHelper;
import net.minecraft.block.Block;
import net.minecraft.block.BlockAir;
import net.minecraft.block.BlockHopper;
import net.minecraft.client.Minecraft;
import net.minecraft.network.play.client.C03PacketPlayer;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.BlockPos;
import net.minecraft.util.MathHelper;
import net.minecraft.util.Timer;
import net.minecraft.world.WorldSettings.GameType;

public class SpartanGlide extends Module {

	int packetsCount;
	int delay = 0;
	  private double start;
	  private int d;
	boolean ground = true;
	public static float speed = 1.0F;
	int clock;
	public static int movement;
	public static Boolean active;
	TimeHelper timer = new TimeHelper();

	public SpartanGlide() {
		super("zSpartan", Keyboard.KEY_NONE, Category.EXPLOITS, false);
	}

	@Override
	public void onUpdate() {
		if (!this.isEnabled)
			return;
		Minecraft.thePlayer.onGround = true;
		Minecraft.thePlayer.capabilities.isFlying = true;
		
		if (active == true) {
			if (clock > 70) {

				if (active == true) {
					ClientUtils.packet(new C03PacketPlayer.C06PacketPlayerPosLook(
							ClientUtils.player().posX + ClientUtils.player().motionX,
							ClientUtils.player().posY
									+ (Minecraft.getMinecraft().gameSettings.keyBindJump.isKeyDown() ? 0.0622 : 0)
									- (Minecraft.getMinecraft().gameSettings.keyBindSneak.isKeyDown() ? 0.0622 : 0),
							ClientUtils.player().posZ + ClientUtils.player().motionZ, ClientUtils.player().rotationYaw,
							ClientUtils.player().rotationPitch, false));
					ClientUtils.packet(new C03PacketPlayer.C06PacketPlayerPosLook(
							ClientUtils.player().posX + ClientUtils.player().motionX,
							ClientUtils.player().posY + ClientUtils.player().motionY - 0.999f,
							ClientUtils.player().posZ + ClientUtils.player().motionZ, ClientUtils.player().rotationYaw,
							ClientUtils.player().rotationPitch, true));
				}
				clock = 0;
			}
			this.movement += 1;

			if (active == true) {
				if (clock > 70) {

					if (active == true) {
						mc.thePlayer.noClip = true;
					}
					clock = 0;
				}
				
				this.delay += 1;
			      if (this.delay > 145)
			      {
			    	  Luna.addChatMessage("Successfully reset all incoming packets!");
			        mc.thePlayer.noClip = true;
			        mc.thePlayer.noClip = false;
			        this.delay = 0;
			      }
			      
			      this.delay += 1;
			      if (this.delay > 55)
			      {
			      //\\  Luna.addChatMessage("Successfully reset all incoming packets!");
			        mc.thePlayer.noClip = true;
			        mc.thePlayer.noClip = false;
			        /** Used for clearing incoming packet queue/server packets [Down below] */
			        zCore.mc().getNetHandler().cleanupzPack();
			        
			        this.delay = 0;
			      }

					this.movement += 1;
						this.packetsCount = 0;
						
						 this.delay = 0;
					}
					if (mc.thePlayer.onGround == true) {
						mc.playerController.setGameType(GameType.CREATIVE);
						mc.playerController.setGameType(GameType.SURVIVAL);
						if (this.movement > 1 && mc.thePlayer.isMoving()) {
							mc.thePlayer.motionX *= 1.005D;
							mc.thePlayer.motionZ *= 1.005D;
							this.movement = 0;
						}
						super.onUpdate();
			}
		}
		float forward = 0.0F;
        float strafe = 0.0F;
        double speed = 0.2D;
       
        float var5 = MathHelper.sin(Minecraft.thePlayer.rotationYaw * 3.1415927F / 180.0F);
        float var6 = MathHelper.cos(Minecraft.thePlayer.rotationYaw * 3.1415927F / 180.0F);
        if(Minecraft.getMinecraft().gameSettings.keyBindAttack.pressed || Minecraft.getMinecraft().gameSettings.keyBindBack.pressed || Minecraft.getMinecraft().gameSettings.keyBindSneak.pressed || Minecraft.getMinecraft().gameSettings.keyBindForward.pressed || Minecraft.getMinecraft().gameSettings.keyBindJump.pressed){
            //Minecraft.getMinecraft().playerController.setGameType(GameType.CREATIVE); Replaced with Clock
        }else{
            if(Minecraft.getMinecraft().gameSettings.keyBindAttack.pressed==false || Minecraft.getMinecraft().gameSettings.keyBindBack.pressed==false || Minecraft.getMinecraft().gameSettings.keyBindSneak.pressed==false || Minecraft.getMinecraft().gameSettings.keyBindForward.pressed==false || Minecraft.getMinecraft().gameSettings.keyBindJump.pressed==false){
                //Minecraft.getMinecraft().playerController.setGameType(GameType.SURVIVAL); Replaced with Clock
            }
        }
       
        if (this.mc.gameSettings.keyBindForward.pressed) {
              forward += 0.1F;
            }
            if (this.mc.gameSettings.keyBindBack.pressed) {
              forward -= 0.1F;
            }
            if (this.mc.gameSettings.keyBindLeft.pressed) {
              strafe += 0.1F;
            }
            if (this.mc.gameSettings.keyBindRight.pressed) {
              strafe -= 0.1F;
            }
        /**
         * Go over holes smoother without lagging everywhere
         */
        if(Minecraft.getMinecraft().gameSettings.keyBindForward.pressed || Minecraft.getMinecraft().gameSettings.keyBindLeft.pressed || Minecraft.getMinecraft().gameSettings.keyBindRight.pressed || Minecraft.getMinecraft().gameSettings.keyBindBack.pressed) {
            zCore.p().motionX = zCore.p().posX * 8.0000000501;
            speed = 0.6952F; //To speed up
            isInsideBlock();
            zCore.p().motionZ = zCore.p().posZ * 8.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
            zCore.p().motionZ = zCore.p().posZ * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
            zCore.p().motionX = zCore.p().posX * 0.0000000501;
           
           
            double motionX = (strafe * var6 - forward * var5) * speed;
            double motionZ = (forward * var6 + strafe * var5) * speed;
            Minecraft.thePlayer.motionX = motionX;
            Minecraft.thePlayer.motionZ = motionZ;
           
            /**
             * Be really careful with changing these numbers.
             */
        }
  	  
        zCore.p().motionY = 0f;
       /**
        * Update 3.1: Sends less packets [clock]
        */
       this.clock += 1;
       if (this.clock >= 10)
    {
           
        zCore.sendPacket(new C03PacketPlayer.C06PacketPlayerPosLook(zCore.p().posX + zCore.p().motionX, zCore.p().posY + (Minecraft.getMinecraft().gameSettings.keyBindJump.pressed ? 0.0622 : 0) - (Minecraft.getMinecraft().gameSettings.keyBindSneak.pressed ? 0.0622 : 0), zCore.p().posZ + zCore.p().motionZ, zCore.p().rotationYaw, zCore.p().rotationPitch, false));
        zCore.sendPacket(new C03PacketPlayer.C06PacketPlayerPosLook(zCore.p().posX + zCore.p().motionX, zCore.p().posY - 0999f, zCore.p().posZ + zCore.p().motionZ, zCore.p().rotationYaw , zCore.p().rotationPitch, true));
        this.clock = 0;
}
}
	
	 @EventTarget
	  public void onBB(BoundingBoxEvent event)
	  {
	    if ((isInsideBlock()) && (event.getBoundingBox() != null) && (event.getBoundingBox().maxY > ClientUtils.player().boundingBox.minY)) {
	      event.setBoundingBox(null);
	    }
	  }
	  
	  @EventTarget
	  public void onPush(PushOutOfBlocksEvent event)
	  {
	    event.setCancelled(true);
	  }
	  
	  @EventTarget
	  private void onInsideBlockRender(InsideBlockRenderEvent event)
	  {
	    event.setCancelled(true);
	  }
	private boolean isInsideBlock() {
		if(this.isEnabled==true && mc.thePlayer.isCollidedHorizontally){
		for (int x = MathHelper.floor_double(ClientUtils.player().boundingBox.minX); x < MathHelper.floor_double(ClientUtils.player().boundingBox.maxX) + 1; x++) {
	  	      for (int y = MathHelper.floor_double(ClientUtils.player().boundingBox.minY); y < MathHelper.floor_double(ClientUtils.player().boundingBox.maxY) + 1; y++) {
	  	        for (int z = MathHelper.floor_double(ClientUtils.player().boundingBox.minZ); z < MathHelper.floor_double(ClientUtils.player().boundingBox.maxZ) + 1; z++)
	  	        {
	  	          Block block = ClientUtils.world().getBlockState(new BlockPos(x, y, z)).getBlock();
	  	          if ((block != null) && (!(block instanceof BlockAir)))
	  	          {
	  	            AxisAlignedBB boundingBox = block.getCollisionBoundingBox(ClientUtils.world(), new BlockPos(x, y, z), ClientUtils.world().getBlockState(new BlockPos(x, y, z)));
	  	            if ((block instanceof BlockHopper)) {
	  	              boundingBox = new AxisAlignedBB(x, y, z, x + 1, y + 1, z + 1);
	  	            }
	  	            if (boundingBox != null) {
	  	              if (ClientUtils.player().boundingBox.intersectsWith(boundingBox)) {
	  	              }
	  	            return true;
	  	            }}
	  	          }
	  	        }
	  	      }
	  	    }
		return false;
	}
	
	@EventTarget
	public void update(final UpdateEvent eventCan){
		if(mc.thePlayer.posY == -0.1f && mc.thePlayer.motionY == -0.f)
		eventCan.setCancelled(true);
	}

	@EventTarget
	public void onPacketRecieve(PacketRecieveEvent event) {
		if (((event.getPacket() instanceof C03PacketPlayer))) {
			event.setCancelled(true);
			event.setCancelled(false);
		}
	}

	@EventTarget
	public void onPacket(PacketSendEvent event) {
		if (((event.getPacket() instanceof C03PacketPlayer))
				|| ((event.getPacket() instanceof C03PacketPlayer.C04PacketPlayerPosition))) {
			if (ClientUtils.player().motionY > -0.45 || ClientUtils.player().motionX > -0.45
					|| ClientUtils.player().motionZ > -0.45) {

			}
			this.packetsCount += 1;
			if (mc.gameSettings.keyBindLeft.pressed || mc.gameSettings.keyBindRight.pressed
					|| mc.gameSettings.keyBindBack.pressed || mc.gameSettings.keyBindForward.pressed) {

				this.packetsCount = 0;
			}
		}
	}

	@EventTarget
	public void onSendPacket(PacketSendEvent event) {
		if (!this.isEnabled)
			return;
		if (((event.getPacket() instanceof C03PacketPlayer)) || ((event.getPacket() instanceof C03PacketPlayer.C04PacketPlayerPosition)))
	    {
	      this.packetsCount += 1;
	        event.setCancelled(true);
	        event.setCancelled(false);
	        this.packetsCount = 0;
	      }else
		if (((event.getPacket() instanceof C03PacketPlayer))
				|| ((event.getPacket() instanceof C03PacketPlayer.C04PacketPlayerPosition))) {
			this.packetsCount += 1;
			if (mc.gameSettings.keyBindLeft.pressed || mc.gameSettings.keyBindRight.pressed
					|| mc.gameSettings.keyBindBack.pressed || mc.gameSettings.keyBindForward.pressed) {

				this.packetsCount = 0;
			}
		}
	}

	@Override
	public void onEnable() {
		active = true;
		super.onEnable();
	}

	@Override
	public void onDisable() {
		mc.playerController.setGameType(GameType.SURVIVAL);
		Minecraft.thePlayer.onGround = false;
		Timer.timerSpeed = 1.0F;
		Minecraft.thePlayer.capabilities.isFlying = false;
		active = false;
		super.onDisable();
	}

	@Override
	public String getValue() {
		return null;
	}

}
